<?php

namespace App\Admin\Controllers;

use App\Models\Region;
use App\Models\Travelnote;
use App\Http\Controllers\Controller;
use App\Models\Wxuser;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class TravelnoteController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('游记管理')
            ->description('')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('游记详情')
            ->description('')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('编辑游记')
            ->description('')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('新增游记')
            ->description('')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Travelnote);

        $grid->id('ID');
        $grid->wxuser_id()->badge();
        $grid->coverimage()->image('http://media.weijingguanjia.com/',36,36);
        $grid->avatar()->image('',36,36);
        $grid->columns('title','author','watch','star','area','spot');
        $grid->recommended()->using([1 => '很差', '2' => '一般',3=>'良好',4=>'满意',5=>'非常满意'])->badge();
        $grid->display('审核')->switch([
            'on'  => ['value' => 1, 'text' => '审核通过', 'color' => 'primary'],
            'off' => ['value' => 0, 'text' => '未审核', 'color' => 'default'],
        ]);
        $grid->score('获取积分')->editable();
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Travelnote::findOrFail($id));

        $show->id('ID');
        $show->created_at('Created at');
        $show->updated_at('Updated at');

        return $show;
    }


    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Travelnote);
        $form->select('area', '区县')->options(Region::all()->pluck('text','text'))->load('spot', '/api/spot');
        $form->select('spot', '风景区');
        $form->text('title','标题')->rules('required');
        $form->text('author','作者')->rules('required');
        $form->ueditor('content','内容')->rules('required');
        $form->image('coverimage','封面')->uniqueName()->removable()->rules('required');
        $form->hidden('is_admin')->default(0);
        $form->switch('display');
        $form->number('score');
        $form->saved(function (Form $form){
            if($form->model()->is_admin==0){
                $form->model()->avatar= asset('/images/avatar.jpg');
                $form->model()->save();
            }

        });
        return $form;
    }
}
